procedure calfib(nome, ver, hor, ext, colavg, colstep, lista)

char    nome         {prompt="CBase name for files "}
int     ver          {1,min=1,max=10,prompt="Number of vertical mask positions"}
int     hor          {5,min=1,max=10,prompt="Number of horizontal mask positions"}
char    ext          {"txt",enum="txt|fits|imh",prompt="Filetype of images"}
int     colavg       {5,min=1,max=15,prompt="Number of columns to average during fits"}
int     colstep      {1,min=1,max=500,prompt="Number of columns to jump from one fit to the next"}
struct  *lista

### *** *** *** DANGER - carefull with the places where I removed SC.

begin

char curr_mask, sc_mask, base_mask, base2_mask

int i,j 

# iraf doesn't like we use the parameter variables within the script
# so here we make a copy of them to a new variable 
# whose name has an n added to the beginning of the name of each of them
# 
int n_ver, n_hor, n_colavg, n_colstep
char n_nome, n_ext

n_ver = ver
n_hor = hor
n_ext = ext
n_nome = nome
n_colavg = colavg
n_colstep = colstep

base_mask = nome

if (access("ifu.conf"))
   delete("ifu.conf",verify=no)

print(n_hor)
print(n_hor,>>"ifu.conf")
print(n_ver,>>"ifu.conf")
### print(base_mask,>>"ifu.conf")
print(base_mask,>>"ifu.conf")
print(n_ext,>>"ifu.conf")
print(n_colavg,>>"ifu.conf")
print(n_colstep,>>"ifu.conf")

# in this loop we go through all aperture database names.
# notice that database names are filenames.  
# We need to convert the IRAF aperture database files into simple tables
# containing 
# number of fibers 
# position width intensity
# position width intensity
# position width intensity
# ...
# which are the .par files for Antonio's gaussian fit program

if ( access("aptog2.csh") )
   delete("aptog2.csh",verify=no)
base2_mask = "database/ap" // nome
for (i=0 ; i<n_ver ; i+=1)
   for (j=0 ; j<n_hor ; j+=1) {
      curr_mask = base2_mask // "-0" // i // "-0" // j 
      sc_mask = base_mask // ".sc-0" // i // "-0" // j // ".par"
print("CURRENTMASK",curr_mask)
      print ("awk -f ${SOAR_IFU}/awk/aptog2.awk ",curr_mask," >! ",sc_mask,>>"aptog2.csh")
      print ("wc -l ",sc_mask," | awk '{print $1}' >! lixo098",>>"aptog2.csh")
      print ("cat lixo098 ",sc_mask,">! lixo099",>>"aptog2.csh")
      print ("mv -f lixo099 ",sc_mask,>>"aptog2.csh")
   }
!csh aptog2.csh
# running lifu will create the tables with all the defined parameters
# the ".ff" files
!lifu NL >& /dev/null

end
